package com.yungu.swift.api.route.v1.passenger.pay.alipay;

import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.route.v1.passenger.pay.tenpay.WxPayApi;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.vo.PayVo;
import com.yungu.swift.order.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Zerrion
 */
@Controller
@Api(value = "【乘客】【支付】【支付宝】乘客支付宝接口", tags = "乘客支付宝支付的相关操作")
public class AliPayApi extends BaseApi {
    private final Logger logger = LoggerFactory.getLogger(AliPayApi.class);

    @Reference
    private PayService payService;

    @RequestMapping(value = "/api/v1/passenger/token/pay/alipay/tradeUrl", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "乘客支付宝支付车费", response = ResponseData.class, notes = "乘客支付宝支付接口")
    public ResponseData<String> tradeUrl(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单号", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "couponUuid", value = "优惠券id") @RequestParam(required = false) String couponUuid,
            HttpServletRequest request) {
        ResponseData<Map<String, String>> trade = payService.trade(new PayVo(OrderConstant.PAY_PURPOSE_CHARGE_FARE,
                CommonConstant.PASSENGER, OrderConstant.PAY_TYPE_ALIPAY, getUserUid(request), orderUuid, couponUuid,
                null, appid, null, null));
        if (trade.isSuccess() && trade.getData() != null) {
            Map<String, String> urlMap = trade.getData();
            return ResponseData.buildSuccessResponse(urlMap.get("url"));
        }
        return ResponseData.buildErrorResponse(trade.getErrCode(), trade.getMsg());
    }

    @ApiOperation(value = "乘客支付宝充值账户", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/api/v1/passenger/token/wallet/walletAliPay"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<String> walletAliPay(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "rechargeId", value = "充值面额Id", required = true) @RequestParam String rechargeId,
            HttpServletRequest request) {
        ResponseData<Map<String, String>> trade = payService.trade(new PayVo(OrderConstant.PAY_PURPOSE_RECHARGE_ACCOUNT,
                CommonConstant.PASSENGER, OrderConstant.PAY_TYPE_ALIPAY, getUserUid(request), null,
                null, rechargeId, appid, null, null));
        if (trade.isSuccess() && trade.getData() != null) {
            Map<String, String> urlMap = trade.getData();
            return ResponseData.buildSuccessResponse(urlMap.get("url"));
        }
        return ResponseData.buildErrorResponse(trade.getErrCode(), trade.getMsg());
    }

}
